package modele;

import java.util.ArrayList;
import java.util.List;

public class City {
	protected String name;
	protected ArrayList<Letter<?>> postbox;

	public City(String n) {
		name = n;
		postbox = new ArrayList<Letter<?>>();
	}

	/**
	 * Send a letter to its receiver.
	 */
	public void sendLetter(Letter<?> l) {
		postbox.add(l);
		System.out.println("---> " + name + "-city added the letter from ["
				+ l.getSender() + "] to [" + l.getReceiver() + "].");
	}

	/**
	 * Distribute letters put in the postbox by inhabitants.
	 */
	public void distributeLetters() {
		if (!postbox.isEmpty())
			System.out.println("\n--------� " + name
					+ " is distributing letters.");
		List<Letter<?>> bag = new ArrayList<Letter<?>>(postbox);
		for (Letter<?> letter : bag) {
			letter.getReceiver().receiveLetter(letter);
			postbox.remove(letter);
		}
	}

	/**
	 * @return name of the city.
	 */
	public String getName() {
		return name;
	}
}
